package com.alex.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.alex.entityVo.FundAuth;
import com.alex.vo.PmUtAidInfoNsfcHosp;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: 葛成瑞
 * @Date: 2025/4/11 14:15
 */
@Service
@Slf4j
public class ParseEntityService {


    public void parseEntity(PmUtAidInfoNsfcHosp pmUtAidInfoNsfcHosp) {
        String keyword = pmUtAidInfoNsfcHosp.getKeyword();
        String newkeyWord = "";
        if (StringUtil.isNotNull(keyword)) {
            List<String> strings = JSONObject.parseArray(keyword, String.class);
            String join = StrUtil.join(";", strings);
            if (join.length() > 0) {
                pmUtAidInfoNsfcHosp.setKeyword(join);
            }
        }
//        String auth = pmUtAidInfoNsfcHosp.getAuth();
//        if (StringUtil.isNotNull(auth)) {
//            // 处理下作者 采用和入库一致策略
//            List<String> strings = JSONObject.parseArray(auth, String.class);
//            List<String> dealUser = new ArrayList<>();
//            for (String string : strings) {
//                String trim = string.trim();
//                // 作者部分其他格式形式 进行适配删除
//                if (trim.contains("(")) {
//                    trim = trim.replace("(", "");
//                }
//                if (trim.contains(")")) {
//                    trim = trim.replace(")", "");
//                }
//                if (StringUtil.isNotNull(trim)&&!trim.contains(",")) {
//                    if (trim.contains("-")) {
//                        trim = trim.replace("-", ", ");
//                    }
//                }
//                if (StringUtil.isNotNull(trim) && trim.contains(",") && trim.contains("-")) {
//                    trim = trim.replace("-", "");
//                }
//                if (StringUtil.isNotNull(trim)) {
//                    dealUser.add(trim);
//                }
//
//            }
//
//            String join = StrUtil.join(";", dealUser);
//            if (join.length() > 0) {
//                pmUtAidInfoNsfcHosp.setAuth(join);
//            }
//        }

        String unitDept = pmUtAidInfoNsfcHosp.getFundAuth();
        Set<String> ausFirst = new LinkedHashSet<>();
        if (StringUtil.isNotNull(unitDept)) {
            Set<String> unitSet = new HashSet<>();
            Set<String> unitDeptSet = new HashSet<>();
            Set<String> deptSet = new HashSet<>();
            List<FundAuth> c1AuthUnitDepts = JSONArray.parseArray(unitDept, FundAuth.class);
            Set<String> aus = new LinkedHashSet<>();
            for (FundAuth c1AuthUnitDept : c1AuthUnitDepts) {
                String unit = c1AuthUnitDept.getUnit();
                List<String> AUS = c1AuthUnitDept.getAuthList();
                unitSet.add(unit);
                List<String> deptList = c1AuthUnitDept.getDeptList();
                for (String s : deptList) {
                    if (StringUtil.isNotNull(s)) {
                        deptSet.add(s);
                        String unitDeptParam = c1AuthUnitDept.getUnit() + "_" + s;
                        unitDeptSet.add(unitDeptParam);
                    }
                }
                if (StringUtil.isNotNull(unit) && AUS.size()>0) {
                    for (String au : AUS) {
                        String trim = au.trim();
                        // 作者部分其他格式形式 进行适配删除
                        if (trim.contains("(")) {
                            trim = trim.replace("(", "");
                        }
                        if (trim.contains(")")) {
                            trim = trim.replace(")", "");
                        }

                        if (StringUtil.isNotNull(trim)&&!trim.contains(",")) {
                            if (trim.contains("-")) {
                                trim = trim.replace("-", ", ");
                            }
                        }
                        if (StringUtil.isNotNull(trim) && trim.contains(",") && trim.contains("-")) {
                            trim = trim.replace("-", "");
                        }
                        if (StringUtil.isNotNull(trim)) {
                            String AUUNIT = trim + "(" + unit.trim() + ")";
                            aus.add(AUUNIT);
                        }
                    }
                }
            }

            // 这里存放的作者(单位)
            if (aus.size() > 0) {
                ausFirst.addAll(aus);
            }

        }
        String joinAuthor = StrUtil.join(";", ausFirst);
        pmUtAidInfoNsfcHosp.setAuth(joinAuthor);
        String pt = pmUtAidInfoNsfcHosp.getPt();
        if (StringUtil.isNotNull(pt)) {
            List<String> strings = JSONObject.parseArray(pt, String.class);
            String join = StrUtil.join(";", strings);
            if (join.length() > 0) {
                pmUtAidInfoNsfcHosp.setPt(join);
            }
        }
        if (StringUtil.isNotNull(pmUtAidInfoNsfcHosp.getTitle())) {
            String s = HtmlUtil.cleanHtmlTag(pmUtAidInfoNsfcHosp.getTitle());
            pmUtAidInfoNsfcHosp.setTitle(s);

        }

    }
}
